//
//  ConnectModel.m
//  UI18_网络封装
//
//  Created by 刘建镇 on 14-12-18.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ConnectModel.h"
#import "Reachability.h"
#import "ToolsModel.h"

@implementation ConnectModel

- (void)dealloc
{
    [_urlName release];

    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {

    }
    return self;
}
//网络判断
+ (BOOL)judgeConnected
{
    Reachability *r = [Reachability reachabilityWithHostname:@"http//:www.baidu.com"];
    NetworkStatus status = [r currentReachabilityStatus];
    switch (status) {
        case NotReachable:
        {
            return NO;
        }
            break;
        case ReachableViaWiFi:
        {
            return  YES;
        }
            case ReachableViaWWAN:
        {
            return YES;
        }
        default:
            break;
    }
}


//为了封装, 所以写了下面的方法, 不用再写协议了.
+ (void)connectModelWithUrlStr:(NSString *)str parmaters:(NSDictionary *)parmaters finished:(ConnectBlock)finishBlock
{
    BOOL judge = [self judgeConnected];
    if (judge) {
        ConnectModel *model = [[ConnectModel alloc] init];
        model.finished = finishBlock;
        [model startConnectWithUrlStr:str parmaters:parmaters];
        [model release];
    }else
    {
        //从本地获取数据
        //以网址为文件名, 名字中不得包含'/'字符
        NSString *name = [ConnectModel urlWithStr:str parmaters:parmaters];
        NSData *data = [ToolsModel dataWithFileName:name];
        //将数据回调到主界面
//        [delegate loadingFinished:data];
        finishBlock(data);
    }
}

//  获取完整的文件名, 以网址作为文件名
+ (NSString *)urlWithStr:(NSString *)urlStr parmaters:(NSDictionary *)parmaters
{
    NSString *myStr = @"";
    for (NSString *key in parmaters) {
        if ([myStr length] == 0) {
            myStr = [NSString stringWithFormat:@"?%@=%@", key, [parmaters objectForKey:key]];
        }else
        {
            myStr = [NSString stringWithFormat:@"%@&%@=%@", myStr, key, [parmaters objectForKey:key]];
        }
    }
    urlStr = [NSString stringWithFormat:@"%@%@", urlStr, myStr];
    //字符串, 将"/"替换成空字符
    urlStr = [urlStr stringByReplacingOccurrencesOfString:@"/" withString:@""];
    return urlStr;
}

- (void)startConnectWithUrlStr:(NSString *)str parmaters:(NSDictionary *)parmaters
{
    NSString *myStr = @"";
    for (NSString *key in parmaters) {
        if ([myStr length] == 0) {
            myStr = [NSString stringWithFormat:@"?%@=%@", key, [parmaters objectForKey:key]];
        }else
        {
            myStr = [NSString stringWithFormat:@"%@&%@=%@", myStr, key, [parmaters objectForKey:key]];
        }
    }
    str = [NSString stringWithFormat:@"%@%@", str, myStr];   
    //urlName用来存储文件的名字
    self.urlName = str;
    
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"GET"];
//    [NSURLConnection connectionWithRequest:request delegate:self];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        self.finished(data);
        NSString *name = [ConnectModel urlWithStr:self.urlName parmaters:nil];
        [ToolsModel saveFile:data fileName:name];
    }];
}
@end
